package com.nkzy.service.impl;

import com.nkzy.mapper.UserMapper;
import com.nkzy.pojo.User;
import com.nkzy.service.UserService;
import com.nkzy.utils.Md5Util;
import com.nkzy.utils.ThreadLocalUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;


@Service
public class UserServiceImpl  implements UserService {
    //在UserService中要调用下层的方法，所以需要依赖注入
    @Resource
    private UserMapper userMapper;
    @Override
    public User findUserByName(String username) {
        User user = userMapper.selectUserByName(username);
        return user;
    }

    @Override
    public int register(String username, String password) {

        //1.对密码加密
        String md5String = Md5Util.getMD5String(password);
        //2.加密后注册
        userMapper.insert(username,md5String);
        return 0;
    }

    @Override
    public int register1(String username, String password) {
        //1.对密码加密
        String md5String = Md5Util.getMD5String(password);
        int i=userMapper.insert02(username,md5String);
        return i;
    }

    @Override
    public void update(User user) {
        user.setUpdateTime(LocalDateTime.now());
        userMapper.updateUser(user);
    }

    @Override
    public void updateAvatar(String avatarUrl) {
        //在Mapper层中需要写更新头像的sql语句，where id=#{id}
        //从ThreadLocal中取出id
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userMapper.updateAvatar(avatarUrl,id);
    }

    @Override
    public void updatePwd(String newPwd) {
        //在Mapper层中需要写更新密码的sql语句，id=#{id}
        Map<String,Object> map=ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        userMapper.updatePwd(Md5Util.getMD5String(newPwd),id);
    }


}
